﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SMS.Common.Silverlight;
using SMS.Controls.CategorySVC;
using SMS.Entities;

namespace SMS.Model
{
    public class CategoryMasterModel
    {
        ServiceProxy<CategoryServiceClient> proxy = null;
        public event Action<int> OnSaveCategoryCompleted;
        public event Action<System.Collections.ObjectModel.ObservableCollection<Category>> OnSearchCompleted;
        public event Action<SMSException> OnServiceError;
        public CategoryMasterModel()
        {
            proxy = new ServiceProxy<CategoryServiceClient>("Service/CategoryService.svc");
            proxy.Client.SaveCategoryCompleted += Client_SaveCategoryCompleted;
            proxy.Client.SearchCategoryCompleted += new EventHandler<SearchCategoryCompletedEventArgs>(Client_SearchCategoryCompleted);
        }

        void Client_SearchCategoryCompleted(object sender, SearchCategoryCompletedEventArgs e)
        {
            if (OnSearchCompleted != null)
                OnSearchCompleted(e.Result);
        }

        void Client_SaveCategoryCompleted(object sender, SaveCategoryCompletedEventArgs e)
        {
            if (e.serviceError == null)
            {
                if (OnSaveCategoryCompleted != null)
                    OnSaveCategoryCompleted(e.Result);
            }
            else if (OnServiceError != null)
                OnServiceError(e.serviceError);
            
        }

        public void SaveCategory(Category category)
        {
            proxy.Client.SaveCategoryAsync(App.LoggedinUserName, category);          
        }

        public void SearchCategory(string searchText)
        {
            proxy.Client.SearchCategoryAsync(App.LoggedinUserName, searchText);
        }
    }
}
